package org.sgx.gwteditors.client.impl1.ui.sbutton;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;
/**
 * a button that is sensitive to when the user is clicking - it has no onclick 
 * but buttonPressed - buttonReleased
 * @author sg
 *
 */
public class SButton extends HTML implements MouseDownHandler, MouseOutHandler, MouseUpHandler, MouseOverHandler {
	private static int counter=0;
	List<SButtonListener> listeners; 
	private static String newId() {
		counter++; 
		return "sbutton"+counter; 
	}
//	private String labelStr="&nbsp;";
	private boolean state;
	private int stateCheckLapsus;
	private Timer mouseTimer;
	
	public SButton(String labelStr, int stateCheckLapsus) {
		super("<div id=\""+newId()+"\">"+labelStr+"</div>");
		listeners = new LinkedList<SButtonListener>(); 
//		this.labelStr = labelStr; 
		this.addMouseDownHandler(this); 
		this.addMouseOutHandler(this); 
		this.addMouseUpHandler(this);
		this.addMouseOverHandler(this); 
		this.getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
		this.getElement().getStyle().setBorderWidth(1, Unit.PX);  
		this.getElement().getStyle().setHeight(5, Unit.PX);
		this.getElement().getStyle().setDisplay(Display.BLOCK); 
		this.getElement().getStyle().setFontSize(9, Unit.PX);
		this.getElement().getStyle().setFontWeight(FontWeight.BOLD); 
		this.getElement().getStyle().setMarginTop(-4, Unit.PX);
		this.getElement().getStyle().setProperty("padding", "0px 2px 5px 2px");
		this.getElement().getStyle().setCursor(Cursor.POINTER); 
		this.stateCheckLapsus=stateCheckLapsus;
		
		mouseTimer = new Timer() {
	      public void run() {
		        if(SButton.this.state) {
		        	notifySButtonChangeState(true);
		        }
		      }
	    };
	}
	public SButton(String labelStr) {
		this(labelStr, 200); 
	}

	public void addSButtonListener(SButtonListener l) {
		listeners.add(l); 
	}
	
	
	private void notifySButtonChangeState(boolean b) {
		for(SButtonListener l : listeners) {
			l.notifyStateChanged(b); 
		}
	}
	@Override
	public void onMouseDown(MouseDownEvent event) {
		notifySButtonChangeState(true); 
		state=true; 
		this.getElement().getStyle().setBackgroundColor("#ff1111"); 

		mouseTimer.scheduleRepeating(stateCheckLapsus);
	}
	
	@Override
	public void onMouseOut(MouseOutEvent event) {
//		if(state==true) 
		state=false; 
		notifySButtonChangeState(false);
		this.getElement().getStyle().setBackgroundColor("#ffffff"); 
		mouseTimer.cancel(); 
	}

	@Override
	public void onMouseUp(MouseUpEvent event) {
		state=false; 
		notifySButtonChangeState(false); 
		this.getElement().getStyle().setBackgroundColor("#ededed");
		mouseTimer.cancel(); 
	}

	@Override
	public void onMouseOver(MouseOverEvent event) {
		this.getElement().getStyle().setBackgroundColor("#ededed"); 
	}
}
